﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace DataCombine
{
    public partial class Form_Chart : Form
    {
        DataTable chartDataSource;
        public Form_Chart()
        {
            InitializeComponent();
        }

        public Form_Chart(DataTable dt)
        {
            chartDataSource = dt;
            InitializeComponent();
        }

        private void Form_Chart_Load(object sender, EventArgs e)
        {
            #region 显示chart
            DataTable dt = chartDataSource;

            if(dt == null)
            {
                MessageBox.Show("未加载数据，无法生成图表");
                return;
            }

            if (dt.Columns.Count <= 1)
            {
                MessageBox.Show("源数据只有小于等于1列，无法生成图表");
                return;
            }

            int sc = chart1.Series.Count;

            chart1.Series[0].Name = dt.Columns[0].ColumnName;

            
            for (int i = sc; i < dt.Columns.Count; i++)
            {
                Console.WriteLine(dt.Columns[i].ColumnName);
                chart1.Series.Add(dt.Columns[i].ColumnName);
            }

            chart1.DataSource = dt;

            chart1.Series[0].XValueMember = dt.Columns[0].ColumnName;

            for(int i = 1; i < dt.Columns.Count; i++)
            {
                Console.WriteLine(dt.Columns[i].ColumnName);
                chart1.Series[i-1].YValueMembers = dt.Columns[i].ColumnName;   
            }

            chart1.DataBind();
            #endregion

            #region combobox data load
            //SeriesChartType
            //comboBox_chart_selections.DataSource = System.Enum.GetNames(typeof(SeriesChartType));
            string[] choiceSeriesChartType = {"Point",
                "FastPoint",
                "Bubble",
                "Line",
                "Spline",
                "StepLine",
                "FastLine",
                "Bar",
                "StackedBar",
                /*"StackedBar100",*/
                "Column",
                "StackedColumn",
                /*"StackedColumn100",*/
                "Area",
                "SplineArea",
                /*"StackedArea",
                "StackedArea100",*/
                "Pie",
                "Doughnut",
                "Stock",
                "Candlestick",
                "Range",
                "SplineRange",
                "RangeBar",
                "RangeColumn",
                "Radar",
                "Polar",
                "ErrorBar",
                "BoxPlot",
                /*"Renko",
                "ThreeLineBreak",
                "Kagi",
                "PointAndFigure",*/
                "Funnel",
                "Pyramid",
                };
            comboBox_chart_selections.DataSource = choiceSeriesChartType;
            /*Console.WriteLine("comboBox_chart_selections:");
            foreach (string s in comboBox_chart_selections.DataSource as string[])
            {
                Console.WriteLine(s);
            }*/
            comboBox_chart_selections.SelectedIndex = 9;
            #endregion


        }

        private DataTable CreateDataTable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Date");
            dt.Columns.Add("Volume1");
            dt.Columns.Add("Volume2");
            dt.Columns.Add("Volume3");

            DataRow dr;

            dr = dt.NewRow();
            dr["Date"] = "Jan";
            dr["Volume1"] = 12333;
            dr["Volume2"] = 4111;
            dr["Volume3"] = 33;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Date"] = "Feb";
            dr["Volume1"] = 7333;
            dr["Volume2"] = 5111;
            dr["Volume3"] = 73;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Date"] = "Mar";
            dr["Volume1"] = 12333;
            dr["Volume2"] = 4111;
            dr["Volume3"] = 33;
            dt.Rows.Add(dr);

            dr = dt.NewRow();
            dr["Date"] = "Apr";
            dr["Volume1"] = 7333;
            dr["Volume2"] = 5111;
            dr["Volume3"] = 73;
            dt.Rows.Add(dr);

            return dt;

        }

        
        private void comboBox_chart_selections_SelectedIndexChanged(object sender, EventArgs e)
        {          
            string selectText = comboBox_chart_selections.SelectedItem.ToString();
            int selectIndex = comboBox_chart_selections.SelectedIndex;
            string log = string.Format("comboBox_chart_selections_SelectedIndexChanged {0} {1}", selectText, selectIndex);
            Console.WriteLine(log);

            try
            {
                chart1.Series[0].ChartType = (SeriesChartType)System.Enum.Parse(typeof(SeriesChartType), selectText);
                chart1.Series[1].ChartType = (SeriesChartType)System.Enum.Parse(typeof(SeriesChartType), selectText);
            } catch
            {
                MessageBox.Show(selectText + " 样式无效");
            }
            
        }

        private void button_save_chart_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Bitmap (*.bmp)|*.bmp|JPEG (*.jpg)|*.jpg|EMF (*.emf)|*.emf|PNG (*.png)|*.png|SVG (*.svg)|*.svg|GIF (*.gif)|*.gif|TIFF (*.tif)|*.tif";
            sfd.FilterIndex = 2;
            sfd.RestoreDirectory = true;
            DialogResult r = sfd.ShowDialog();

            if (r == DialogResult.OK)
            {
                ChartImageFormat format = ChartImageFormat.Bmp;

                if (sfd.FileName.EndsWith("bmp"))
                {
                    format = ChartImageFormat.Bmp;
                }
                else if (sfd.FileName.EndsWith("jpg"))
                {
                    format = ChartImageFormat.Jpeg;
                }
                else if (sfd.FileName.EndsWith("emf"))
                {
                    format = ChartImageFormat.Emf;
                }
                else if (sfd.FileName.EndsWith("gif"))
                {
                    format = ChartImageFormat.Gif;
                }
                else if (sfd.FileName.EndsWith("png"))
                {
                    format = ChartImageFormat.Png;
                }
                else if (sfd.FileName.EndsWith("tif"))
                {
                    format = ChartImageFormat.Tiff;
                }
                /*else if (sfd.FileName.EndsWith("svg"))
                {
                    format = ChartImageFormat.Svg;
                }*/

                // Save image
                chart1.SaveImage(sfd.FileName, format);
            }
        }


        private void button_CopyToClipboard_Click(object sender, EventArgs e)
        {
            string folderName = Properties.Settings.Default.ClipboardTempFolderPath;
            if (!Directory.Exists(folderName))
            {
                Directory.CreateDirectory(folderName);
            }

            string tmpFileName = folderName+ "/" + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".bmp";
            chart1.SaveImage(tmpFileName, ChartImageFormat.Bmp);

            if (File.Exists(tmpFileName))
            {
                Image img = Image.FromFile(tmpFileName);
                Clipboard.SetImage(img);
            } 
            else
            {
                MessageBox.Show("复制到剪切板失败");
            }
            
        }
    }
}
